package com.maoye.ossp.biz.brand.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.maoye.ossp.biz.brand.entity.BrandPic;
import com.maoye.ossp.biz.brand.service.IBrandPicService;
import com.maoye.ossp.biz.brand.utils.BrandCommonConstant;
import com.maoye.ossp.biz.brand.utils.BrandUtils;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;

/**
 * 对象功能:品牌责任人
 * 开发公司:MOI
 * 开发人员:liuyp2
 * 创建时间:2014-02-12 16:59:38
 */
@Controller
@RequestMapping("biz/brand/pic/")
public class BrandPicController extends BaseController
{
	@Resource
	private IBrandPicService brandPicService;
	
	@RequestMapping(value = "list")
	public String list(BrandPic brandPic, HttpServletRequest request,
		HttpServletResponse response, Model model) {
		QueryFilter queryFilter = getQuerFilter(request, brandPic);
		Page<BrandPic> page = brandPicService.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/biz/brand/pic/list", model, request);
	}
		
	@RequestMapping(value = "save")
	public String save(BrandPic brandPic, Model model,
			RedirectAttributes redirectAttributes,HttpServletRequest request) 
	{
		String resultPage = resultPage("redirect:/biz/brand/pic/list", model, request);
		try
		{
			String msg = brandPicService.save(brandPic);
			if(null != msg && !"".equals(msg))
			{
				addMessage(model, msg);
				model.addAttribute("brandPic", brandPic);
				resultPage = "/biz/brand/pic/add";
			}else
			{
				addMessage(model, BrandCommonConstant.MSG_2002);
			}
		}
		catch(Exception e)
		{
			addMessage(model, BrandCommonConstant.EX_1009);
			resultPage =  "/biz/brand/pic/add";
		}
		return resultPage;
	}
	
	@RequestMapping(value = "add")
	public String add(BrandPic brandPic, Model model) 
	{
		model.addAttribute("brandPic", brandPic);
		return "/biz/brand/pic/add";
	}

	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable String id, Model model) 
	{
		BrandPic brandPic = brandPicService.get(Long.valueOf(id));
		model.addAttribute("brandPic", brandPic);
		return "/biz/brand/pic/add";
	}

	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable String id, Model model) 
	{
		BrandPic brandPic = brandPicService.get(Long.valueOf(id));
		model.addAttribute("brandPic", brandPic);
		return "/biz/brand/pic/view";
	}

	@RequestMapping(value = "{id}/delete")
	public String delete(@PathVariable String id, Model model,
			RedirectAttributes redirectAttributes) 
	{
		brandPicService.remove(Long.valueOf(id));
		addMessage(redirectAttributes, BrandCommonConstant.MSG_2006);
		return "redirect:/biz/brand/pic/list";
	}
	
	@RequestMapping(value = "deleteAll")
	public String deleteAll(HttpServletRequest request,
			HttpServletResponse response, Model model,RedirectAttributes redirectAttributes) 
	{
		String deleteIds = request.getParameter("deleteIds");
		try 
		{
			Long[] lAryId = BrandUtils.getLongAryByStr(deleteIds,";");
			for (Long id : lAryId) {
				brandPicService.remove(id);
			}
			addMessage(redirectAttributes, BrandCommonConstant.MSG_2006);
		} catch (Exception e) {
			addMessage(redirectAttributes, BrandCommonConstant.EX_1012);
		}
		return "redirect:/biz/brand/pic/list";
	}
	
	@RequestMapping(value = "{id}/sap")
	public String sendSap(@PathVariable String id, Model model,
			RedirectAttributes redirectAttributes) {
		BrandPic brandPic = brandPicService.get(Long.valueOf(id));
		String msg = "";
		try
		{
			msg = brandPicService.sendToSAP(brandPic);
		}catch(Exception e)
		{
			e.printStackTrace();
			msg = BrandCommonConstant.EX_1008+e.getCause();
		}
		addMessage(redirectAttributes, msg);
		model.addAttribute("brandPic", brandPic);
		model.addAttribute("msg", msg);
		return "/biz/brand/pic/view";
	}
	
	@RequestMapping(value = "sapAll")
	public String sendSapAll(HttpServletRequest request,
			HttpServletResponse response, Model model,RedirectAttributes redirectAttributes)  {
		String sendIds = request.getParameter("sendIds");
		Long[] ids = BrandUtils.getLongAryByStr(sendIds,";");
		String msg = "";
		try
		{
			msg = brandPicService.sendToSAP(ids);
		}catch(Exception e)
		{
			e.printStackTrace();
			msg = BrandCommonConstant.EX_1008+e.getCause();
		}
		addMessage(redirectAttributes, msg);
		return "redirect:/biz/brand/pic/list";
	}
	
	@RequestMapping(value = "synchroniSap")
	public String synchroniSap(HttpServletRequest request,
			HttpServletResponse response, Model model,RedirectAttributes redirectAttributes)  {
		String msg = "";
		try
		{
			msg = brandPicService.synchroniSap(new BrandPic());
		}catch(Exception e)
		{
			e.printStackTrace();
			msg = BrandCommonConstant.EX_1008+e.getCause();
		}
		addMessage(redirectAttributes, msg);
		return "redirect:/biz/brand/excel/syn";
	}
}
